#ifndef _D2PLATFORM_H_
#define _D2PLATFORM_H_

#include <vector>
#include <windows.h>
#include "ball.h"
#include "D2Ball.h"
#include "hole.h"
#include "state.h"





class Rect {

public:
	Rect(int x, int y, int w, int h):byteperpixel(4), left(x), top(y), right(x+w), bottom(y+h), width(w), height(h){
		linesize = byteperpixel * w;
		size = h * w * byteperpixel;
		numofcircle = 1;
		pixelofcircle = 0;
	};
	Rect():byteperpixel(4){
		left = top = right = bottom = -1;
		pixelofcircle =0;
	};
	const int byteperpixel;
	int left, top;
	int right, bottom;
	int width, height;
	int linesize;
	int size;
	int numofcircle; //how many circles in this rect
	int pixelofcircle; //the number of pixel in this circles
};


class Color{
public:
	static const unsigned char background = '1';
	static const unsigned char brown = 'h';
	static const unsigned char red = 'r';
	static const unsigned char pink = 'p';
	static const unsigned char blue = 'o';
	static const unsigned char yellow = 'y';
	static const unsigned char black = 'b';
	static const unsigned char white = 'w';
	static const unsigned char green = 'g';
	static const unsigned char unkown = '0';
};



class D2Platform {

public:
	D2Platform(int x, int y, int w, int h);
	~D2Platform();
	void myTurn();
	void goodBall();

	int init();
	int capture(std::shared_ptr<char>& buff, int len, const Rect& rect);
	int parserImage(std::shared_ptr<char>& buff, const int& height, const int& width, Rect* table, std::vector<std::shared_ptr<Ball> >& vB, std::vector<std::shared_ptr<Ball> >& vH, Ball& whiteBall);
	
	int calc(Point& pnt);
	int shooter(const Point& pnt); //move (x,y) and shooter a time;
	int stage();
private:
	void dfs(unsigned char*flag, bool* visit, const int&w, const int&h, const int&x, const int&y, Rect& r);
	void colorRecognition(unsigned char* image, const int& width, const int& height, unsigned char* flag);
	void debugImage();
	void hough(unsigned char* flag, const int& imageWidth, const Rect& rect, std::vector<std::shared_ptr<Ball>>& vB, const bool&);
	void findBallRect(unsigned char* flag, const int& width, const int& height, std::vector<Rect>&);
	void calcColorFromHsv(unsigned char* flag, int idx, unsigned char* src);
	void findHoleRect(unsigned char* flag, const int& width, const int& height, std::vector<Rect>& vR);

	int calcBumpPoint(const std::shared_ptr<D2Ball>& ball, const std::shared_ptr<Hole>& hole, Point& pnt);
	void findColorDfs(unsigned char* flag, bool* visit, const int& w, const int& x, const int& y, int& count, unsigned char& c, std::shared_ptr<Ball>& ball);
	void calcAllBumpPoint(const std::vector<Point>& vcPoint, const std::vector<std::shared_ptr<Ball>>& vB, const D2Ball& whiteBall, std::vector<Point>& bumpPoint);
	void calcBallToHolePoint(const std::vector<std::shared_ptr<Ball>>& vB, const std::vector<std::shared_ptr<Ball>>& vH, std::vector<Point>& vcPoint);

	Rect table;
	D2Ball whiteBall;
	std::vector<std::shared_ptr<Ball> > vBalls;
	std::vector<std::shared_ptr<Ball> > vHoles;
	
	//image
	static const int byteperpixel = 4;
	int imageW, imageH, imageX, imageY; 
	int bufferLen;   //bufferLen = iamgeW * imageH * 4
	std::shared_ptr<char> buffImage; //std::shared_ptr<char> buffImage, smart point did not support allocating a array



	//State
	State st;
};





#endif